﻿#region References
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using LuaInterface;
using Ness.Engine.GUI;
#endregion

namespace Ness.Engine.GUI
{
    /// <summary>
    /// Allows keys to be bound to a Lua function
    /// </summary>
    static class Binds
    {
        #region Public variables
        public static Hashtable bindList;
        #endregion

        /// <summary>
        /// Adds a bind to the list
        /// </summary>
        /// <param name="key">Typeable key - EG: K</param>
        /// <param name="func">Lua function to execute</param>
        public static void AddBind(string key, object func)
        {
            if (bindList == null) bindList = new Hashtable();
            if (bindList.ContainsKey(key))
            {
                bindList.Remove(key);
            }

            //Add the bind to hashtable
            bindList.Add(key, func);
        }

        /// <summary>
        /// Execute the keybind
        /// </summary>
        /// <param name="key">Key to find</param>
        public static void DoBind(string key)
        {
            try
            {
                if (bindList.ContainsKey(key))
                {
                    object func = bindList[key];
                    if (func is LuaFunction)
                    {
                        //Execute the Lua function
                        (func as LuaFunction).Call();
                    }
                }
            }
            catch (Exception e)
            {
                Debugger.WriteError(e.Message);
            }
        }

    }
}
